/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ac.manchester.cs.afzal.tcm_processing.nlp;

/*
 * Date Modified: 18 July, 2013
 */

import java.io.File;
import ac.manchester.cs.afzal.tcm_processing.miscellaneous.File_Manager_Impl;
import java.io.FileInputStream;
import java.util.Properties;

public class Parse_Chinese_Medicine_XML
{

    File_Manager_Impl fileManager = new File_Manager_Impl();
    private String DOCUMENTS_CORPUS = "DOCUMENTS_CORPUS";

    private final String EXPRESSIONS_TO_BE_REMOVED[] =
    {
        "<sec>", "</sec>", "<st>", "</st>", "<it>", "</it>", "<abbrgrp>",
        "</abbrgrp>", "&#8226;", "&#160;",
        "<abbr bid=\"(.){0,3}\">(.){0,3}</abbr>",
        "<figr fid=\"(.){0,3}\">(.){0,7}</figr>",
        "<tblr tid=\"(.){0,3}\">(.){0,7}</tblr>",
        "<supplr sid=\"(.){0,3}\">(.){0,7}</supplr>"


    };
    private final String TAGS_TO_REPLACE_WITH_NEWLINE[] =
    {
        "<p>", "</p>"
    };
    private final String TAGS_TO_REPLACE_WITH_SPACE[] =
    {
      "<b>", "</b>", "</title>", "</p>", "</sec>", "<sup>", "</sup>", "<sub>", "</sub>",
        "   "
    };

 //  File_Manager_Impl fileManager;
    String_Manipulator_Impl strMan = new String_Manipulator_Impl();


    public Parse_Chinese_Medicine_XML()
    {
        Properties props = new Properties();
        try
        {
            props.load(new FileInputStream("Term_Classification.properties"));
        }
        catch (Exception ex)
        {
            System.out.println("Exception while loading propoerties : " + ex.getCause());
        }
        DOCUMENTS_CORPUS = props.getProperty(DOCUMENTS_CORPUS);
    }

    /**
     * Converts the XML files of PubMedCentral XML format to text
     */
    public void convert_pmc_xml_to_text()
    {

        File dir = new File(DOCUMENTS_CORPUS);
        String[] str = dir.list();

        File xmlFile;

        for(String inputFileName : str)
        {
            xmlFile = new File(DOCUMENTS_CORPUS + inputFileName);
            String outputFileName = DOCUMENTS_CORPUS + xmlFile.getName();
            outputFileName = outputFileName.replace("xml", "txt");

            StringBuffer contents = new StringBuffer();
            StringBuffer parsed_contents = new StringBuffer();
            contents = fileManager.fileReader(xmlFile);


            String title = strMan.string_extractor(contents.toString(), "<title>", "</title>");
            title = strMan.string_extractor(title.toString(), "<p>", "</p>");
            title = remove_references(title);
            parsed_contents.append(title);
            parsed_contents.append(System.getProperty("line.separator"));


            String pid = strMan.string_extractor(contents.toString(), "idtype=\"pmpid\">", "</pubid>");

            int index = 0;
            int temp = 0;

            index = contents.indexOf("<abs>", index);
            index = index + 153;
            temp = index;
            index = contents.indexOf("</abs>", index);

            if(index != -1)
            {
                String abstract_ = contents.substring(temp, index);
                abstract_ = remove_references(abstract_);
                abstract_ = extract_heading(abstract_);
                parsed_contents.append(abstract_);
            }


            String trimmed_text = strMan.string_extractor(contents.toString(), "<bdy>", "</bdy>");

            trimmed_text = remove_references(trimmed_text);
            trimmed_text = extract_heading(trimmed_text);

            parsed_contents.append(trimmed_text);
            fileManager.fileWriter(parsed_contents,  outputFileName);
        }

    }

    private String remove_references(String str)
    {

        for(String patternStr : EXPRESSIONS_TO_BE_REMOVED)
        {
            str = strMan.pattern_replacer(str, patternStr, "");
        }
        return str;
    }

    /*
     *
     */
    private String extract_heading(String str)
    {
        for(String patternStr : TAGS_TO_REPLACE_WITH_NEWLINE)
        {
            str = strMan.pattern_replacer(str, patternStr, "\n");
        }

        for(String patternStr : TAGS_TO_REPLACE_WITH_SPACE)
        {
            str = strMan.pattern_replacer(str, patternStr, "");
        }

        str = strMan.string_replacer_start_zero(str, "<url>", "</url", "");
        str = strMan.string_replacer_start_zero(str, "<abbr bid", "</abbr>", "");
        str = strMan.string_replacer_start_zero(str, "<inline-formula>", "</inline-formula>", "");
        str = strMan.string_replacer_start_zero(str, "<display-formula", "</display-formula>", "");
        str = strMan.string_replacer_start_zero(str, "<m:math", "</m:math>", "");
        str = strMan.string_replacer_start_zero(str, "<graphic file", "/>", "");
        str = strMan.string_replacer_start_zero(str, "<fig id=", "</fig>", "");
        str = strMan.string_replacer_start_zero(str, "<tbl id=", "</tbl>", "");
        str = strMan.string_replacer_start_zero(str, "<suppl id=", "</suppl>", "");
        str = strMan.string_replacer_start_zero(str,"<abbr", "</abbr>", "");
        str = strMan.string_replacer_start_zero(str,"<ext-link", ">", "");
        str = strMan.string_replacer_start_zero(str,"<figr", "figr>", "");

        return str;
    }

}
